package com.runa.monitor.platinterface.extend.httpSimilar.bjjn;

import com.runa.monitor.platinterface.meter3_0.util.MySqlJdbcUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Slf4j
public class SqlServerConnection {


    private String driver;
    private String url;
    private String username;
    private String password;

    public SqlServerConnection(String driver, String url, String username, String password) {
        this.driver = driver;
        this.url = url;
        this.username = username;
        this.password = password;
    }

    @SneakyThrows
    public static void main(String[] args) {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection rn = DriverManager.getConnection(
                "jdbc:sqlserver://c.bjjnrl.net:7002;DatabaseName=master", "rn", "rn@123!");
//                "jdbc:sqlserver://c.bjjnrl.net,7002", "rn", "rn@123!");
        Connection connection = rn;
        System.out.println(2);
    }


    private Connection getConnection() {
        log.info("XXXXXXXXXXXX---BJJN-Panel SqlServer开始获取连接---XXXXXXXXXXXX");
        Connection connection = null;
        for (int i = 0; i < 2; i++) {
            try {
//                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                Class.forName(driver);
                connection = DriverManager.getConnection(url, username, password);
                if (connection != null) {
                    log.info("XXXXXXXXXXXX---BJJN-Panel SqlServer成功获取连接---XXXXXXXXXXXX");
                    break;
                }
                TimeUnit.SECONDS.sleep(3000);
            } catch (Exception e) {

                log.error("XXXXXXXXXXXX---BJJN-Panel SqlServer获取连接异常---XXXXXXXXXXXX");
            }
        }
        return connection;
    }

    public ArrayList<HashMap<String, Object>> query(String sql, Object... params) {
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        Connection connection = null;
        try {
            preparedStatement = getConnection().prepareStatement(sql);
            MySqlJdbcUtil.setPreparedStatement(preparedStatement, params);
            resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData(); // key name
            int columnCount = metaData.getColumnCount(); // column count
            ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
            while (resultSet.next()) {
                HashMap<String, Object> rowData = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; ++i) {
                    rowData.put(metaData.getColumnLabel(i), resultSet.getObject(i));
                }
                list.add(rowData);
            }
            return list;
        } catch (Exception e) {
            log.error("XXXXXXXXXXXX---BJJN-Panel SqlServer query调试信息---XXXXXXXXXXXX" + sql + "\r\n" + ExceptionUtils.getFullStackTrace(e), e);
        } finally {
            MySqlJdbcUtil.close(resultSet, preparedStatement, connection);
        }
        return new ArrayList<>();
    }

}
